home *** CD-ROM | disk | FTP | other *** search
- unit EventU4;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls;
-
- type
- TForm1 = class(TForm)
- CheckBox1: TCheckBox;
- Button1: TButton;
- procedure Button1Click(Sender: TObject);
- procedure CheckBox1Click(Sender: TObject);
- private
- { Private declarations }
- public
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- uses
- TypInfo;
-
- procedure SetHandler(AObject: TObject; Event: String; Enable: Boolean);
- const
- { Used for saving old event handler }
- OldHandler: TMethod = (Code: nil; Data: nil);
- { Used for disabling event handler }
- NilMethod: TMethod = (Code: nil; Data: nil);
- var
- PropInfo: PPropInfo;
- begin
- PropInfo := GetPropInfo(AObject.ClassInfo, Event);
- if not Assigned(PropInfo) then
- raise Exception.CreateFmt(
- 'Event %s not found in %s class', [Event, AObject.ClassName]);
- with PropInfo^ do
- begin
- if PropType^.Kind <> tkMethod then
- raise Exception.CreateFmt('%s is not an event', [Event]);
- if Enable then
- SetMethodProp(AObject, PropInfo, OldHandler)
- else
- begin
- OldHandler := GetMethodProp(AObject, PropInfo);
- SetMethodProp(AObject, PropInfo, NilMethod)
- end
- end
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- SetHandler(CheckBox1, 'OnClick', False);
- try
- { This won't trigger the event since it has been disabled }
- CheckBox1.Checked := not CheckBox1.Checked
- finally
- SetHandler(CheckBox1, 'OnClick', True)
- end
- end;
-
- procedure TForm1.CheckBox1Click(Sender: TObject);
- begin
- ShowMessage('The checkbox was clicked by the user');
- { Blah, blah, blah }
- end;
-
- end.
-